package com.iqiju.guagua.redtone.video.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iqiju.guagua.redtone.video.mapper.WxMiniVideoMapper;
import com.iqiju.guagua.redtone.video.model.WxMiniVideo;
import com.iqiju.guagua.redtone.video.utils.StringUtils;
import com.iqiju.guagua.redtone.video.vo.VideoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class WxMiniVideoService extends ServiceImpl<WxMiniVideoMapper, WxMiniVideo> {

    private static final Map<String,String> REDTONE_VIDEO_VIDEO = new HashMap<>(131072);



    /** 根据视频ids查询所有视频对象 缓存在本地map中 */
    public List<VideoVO> selectListByIds(List<Long> ids) {
        List<VideoVO> videoVOS = new ArrayList<>();

        for (Long id : ids) {
            VideoVO videoVO = null;
            String videoVOValue = REDTONE_VIDEO_VIDEO.get(getVideoKey(id));
            if (StringUtils.isNotNull(videoVOValue)){
                videoVO = JSON.parseObject(videoVOValue,VideoVO.class);
                videoVOS.add(videoVO);
                continue;
            }
            videoVO = new VideoVO();
            WxMiniVideo wxMiniVideo = getById(id);
            videoVO.setType(1);
            videoVO.setId(wxMiniVideo.getId());
            videoVO.setName(wxMiniVideo.getName());
            videoVO.setPicUrl(wxMiniVideo.getPicSmall());
            videoVO.setVideoUrl(wxMiniVideo.getUrl());
            REDTONE_VIDEO_VIDEO.put(getVideoKey(id),JSON.toJSONString(videoVO));
            videoVOS.add(videoVO);
        }
        return videoVOS;
    }


    private String getVideoKey(Long id){
        return "REDTONE_VIDEO_VIDEO_" + id;
    }
}
